/*This file is prepared for Doxygen automatic documentation generation.*/
//! \file *********************************************************************
//!
//! \brief This file manages the USB Host mouse control application.
//!
//! - Compiler:           IAR EWAVR and GNU GCC for AVR
//! - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
//!
//! \author               Atmel Corporation: http://www.atmel.com \n
//!                       Support and FAQ: http://support.atmel.no/
//!
//! ***************************************************************************
/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an Atmel
 * AVR product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//_____  I N C L U D E S ___________________________________________________
#include "conf/config.h"
#include "conf/conf_usb.h"
#include "host_mouse_task.h"
#include "host_application.h"
#include "modules/usb/host_chap9/usb_host_task.h"
#include "modules/usb/host_chap9/usb_host_enum.h"
#include "lib_mcu/usb/usb_drv.h"

//_____ M A C R O S ________________________________________________________

#ifndef LOG_STR_CODE
#define LOG_STR_CODE(str)
#else
U8 code log_mouse_connect[]="Mouse Connected";
U8 code log_keyboard_connect[]="Keyboard Connected";
#endif

//_____ D E F I N I T I O N S ______________________________________________

//_____ D E C L A R A T I O N S ____________________________________________


U8 saved_device;
#if(USB_MOUSE_FEATURE == ENABLED)
U8 mouse_connected = 0;
U8 pipe_mouse_in;
#endif
#if(USB_KEYBOARD_FEATURE == ENABLED)
U8 keyboard_connected=0;
U8 pipe_keyboard_in;
#endif

//!@brief This function initializes the Host USB mouse application
//!
//!
//!@param none
//!
//!@return none
void host_mouse_task_init(void)
{
	app_update_table_size();
#if(USB_MOUSE_FEATURE == ENABLED)
	app_calc_lut();
#endif
	Leds_init();
}

//! @brief This function manages the HOST mouse application
//!
//! @param none
//!
//! @return none
void host_mouse_task(void)
{
	U8 i;
	U8 j;
#if(DEBUG_LED_APP_1 == ENABLED)
	S8 new_x, new_y;
#endif

	//Test HID device connection
	if (Is_host_ready())		// fertig mit Enumeration von neum Device?
	{
		if (Is_new_device_connection_event())   //Device connection
		{
#if(USB_MOUSE_FEATURE == ENABLED)
			mouse_connected = 0;
#endif
#if(USB_KEYBOARD_FEATURE == ENABLED)
			keyboard_connected = 0;
#endif

#if(USB_MOUSE_FEATURE == ENABLED)
			saved_device = selected_device;      // Backup user selected device
			for (j = 0; j < Host_get_nb_device() ; j++)
			{
				Host_select_device(j);
				for (i = 0; i < Get_nb_supported_interface(); i++)
				{
					if (Get_class(i)==HID_CLASS && Get_protocol(i)==HID_PROTOCOL_MOUSE)
					{
						mouse_connected=1;
						host_hid_set_idle();
						host_get_hid_repport_descriptor();
						LOG_STR_CODE(log_mouse_connect);
						PIPE_MOUSE_IN=host_get_hwd_pipe_nb(Get_ep_addr(i,0));
						Host_select_pipe(PIPE_MOUSE_IN);
						Host_continuous_in_mode();
						Host_unfreeze_pipe();
						goto mouse_connection_ready;
						//break;
					}
				}
			}
			mouse_connection_ready: Host_select_device(saved_device);
			// Restore user selected device
#endif

#if(USB_KEYBOARD_FEATURE == ENABLED)
			for (i = 0; i < Host_get_nb_device();i++)
			{
				if(Get_class(i)==HID_CLASS && Get_protocol(i)==HID_PROTOCOL_KEYBOARD)
				{
					keyboard_connected=1;
					host_hid_set_idle();
					host_get_hid_repport_descriptor();
					LOG_STR_CODE(log_keyboard_connect);
					PIPE_KEYBOARD_IN=host_get_hwd_pipe_nb(Get_ep_addr(i,0));
					Host_select_pipe(PIPE_KEYBOARD_IN);
					Host_continuous_in_mode();
					Host_unfreeze_pipe();
					break;
				}
			}
#endif
		}

#if(USB_MOUSE_FEATURE == ENABLED)
		if (mouse_connected)
		{
			Host_select_pipe(PIPE_MOUSE_IN);
			if (Is_host_in_received())
			{
				if (Is_host_stall() == FALSE)
				{
					app_read_report();

					//app_update_mouse_buttons();
					app_update_right_stick();

					app_send_report();



/*					if(serial_report.Bt & DS3_UP)
						Led0_on();
					if(serial_report.Bt & DS3_TRIANGLE)
						Led1_on();
					if(serial_report.Bt & DS3_R1)
						Led2_on();
					if(serial_report.Bt & DS3_R2)
						Led3_on();

					if(serial_report.Bt & DS3_R3)
						Led1_off();
					if(serial_report.Bt & DS3_L1)
						Leds_off();*/

#if(DEBUG_LED_APP_1 == ENABLED)
					i = Host_read_byte();
					new_x = (S8) Host_read_byte();
					new_y = (S8) Host_read_byte();

					if (new_x == 0)
					{
						Led0_off();
						Led1_off();
					}
					else if (new_x > 0)
					{
						Led0_on();
						Led1_off();
					}
					else
					{
						Led0_off();
						Led1_on();
					}
					if (new_y == 0)
					{
						Led2_off();
						Led3_off();
					}
					else if (new_y > 0)
					{
						Led2_on() Led3_off();
					}
					else
					{
						Led2_off();
						Led3_on();
					}
#endif
				}
				Host_ack_in_received();
				Host_send_in();
			}
			if (Is_host_nak_received())
			{
#if(DEBUG_LED_APP_1 == ENABLED)
				Leds_off();
#endif
				Host_ack_nak_received();
			}
		}
#endif

#if(USB_KEYBOARD_FEATURE == ENABLED)
		if(keyboard_connected)
		{
			Host_select_pipe(PIPE_KEYBOARD_IN);
			if(Is_host_in_received())
			{
				if(Is_host_stall()==FALSE)
				{
					app_read_report();
					app_update_keyboard_buttons();
					//app_update_left_stick();
				}
				Host_ack_in_received(); Host_send_in();
			}
			if(Is_host_nak_received())
			{
				Host_ack_nak_received();
			}
		}
#endif
	}

	//Device disconnection...
	if (Is_device_disconnection_event())
	{
		Leds_off();
#if(USB_MOUSE_FEATURE == ENABLED)
		mouse_connected = 0;
#endif
#if(USB_KEYBOARD_FEATURE == ENABLED)
		keyboard_connected = 0;
#endif
	}
}
